import { ElNotification } from "element-plus";

// ElNotification.js
/**
 * @Description: 重写ElNotification挂载，实现 Class 的私有属性
 * @param { String } options => 消息内容
 * @param { Boolean } single => 是否只显示一个
 */

const showMessage = Symbol("showMessage");
const titleList = new Map([
  ["error", "错误"],
  ["success", "成功"],
  ["warning", "警告"],
  ["info", "提示"]
]);

class NotifMessage {
  success(options, single = false) {
    this[showMessage]("success", options, single);
  }
  warning(options, single = false) {
    this[showMessage]("warning", options, single);
  }
  info(options, single = false) {
    this[showMessage]("info", options, single);
  }
  error(options, single = true) {
    this[showMessage]("error", options, single);
  }

  [showMessage](type, options, single) {
    if (single) {
      // 判断是否已存在ElNotifications
      if (document.getElementsByClassName("el-notification").length === 0) {
        ElNotification[type]({
          message: options,
          title: titleList.get(type),
          offset: 60,
          duration: 2000
        });
      }
    } else {
      ElNotification[type]({
        message: options,
        title: titleList.get(type),
        offset: 60,
        duration: 2000
      });
    }
  }
}

// 默认导出 私有 Message 组件
export default new NotifMessage();
